<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class ArticleflowControllerArticleflow extends ArticleflowController {

    function __construct($config = array()) {
        parent::__construct($config);

        $this->registerTask('add', 'display');
        $this->registerTask('edit', 'display');

        // Process the submitted data
    }

    function display($cachable = false) {

        switch ($this->getTask()) {
            case 'add': {
                    JRequest::setVar('hidemainmenu', 1);
                    JRequest::setVar('layout', 'form');
                    JRequest::setVar('view', 'articleflow_edit');
                    JRequest::setVar('edit', false);
                }break;
            case 'edit': {
                    JRequest::setVar('hidemainmenu', 1);
                    JRequest::setVar('layout', 'form');
                    JRequest::setVar('view', 'articleflow_edit');
                    JRequest::setVar('edit', true);
                }
                break;

            default : {
                    JRequest::setVar('view', 'articleflow');
                }break;
        }
        parent::display($cachable);
    }

    function save() {

        JRequest::checkToken() or jexit('Invaild Token');
        $user = & JFactory::getUser();
        $post = JRequest::get('post');
        $cid = JRequest::getVar('cid', array(0), 'post', 'array');
        $post['id'] = (int) $cid[0];
        // print_r($post); die();
        $post['introtext'] = JRequest::getVar('introtext', '', 'post', 'string', JREQUEST_ALLOWRAW);
        $post['fulltext'] = JRequest::getVar('fulltext', '', 'post', 'string', JREQUEST_ALLOWRAW);
        $role = ArticleflowHelper::getUserRole();
        $post['assigned_user'] = $user->id;
        // print_r(); die();
        if ($role == 1) {
            $post['state'] = 1;
        } else if ($role == 2) {
            $post['state'] = 2;
        }else
            $post['state'] = 0;

        if ($post['id'] != 0) {
            $post['modified'] = gmdate('Y-m-d H:i:s');
            $post['modified_by'] = (int) $user->get('id');
        } else {

            $post['created'] = gmdate('Y-m-d H:i:s');
            $post['created_by'] = (int) $user->get('id');
        }

        $model = $this->getModel('articleflow_edit');
        if ($model->store($post)) {
            $msg = JText::_('Article Flow Saved');
        } else {
            $msg = JText::_('Error Saving Article Flow');
        }

        $this->setRedirect('index.php?option=com_articleflow', $msg);
    }

    // Luu vao bang jos_content
    function saveComplete() {
        JRequest::checkToken() or jexit('Invaild Token');
        $user = & JFactory::getUser();
        $post = JRequest::get('post');
        $cid = JRequest::getVar('cid', array(0), 'post', 'array');
        JArrayHelper::toInteger($cid);
        //echo $cid[0]; die();
        $content_temp_id['id'] = (int) $cid[0];
        // echo $content_temp_id;
        //  die();
        $post['id'] = 0;

        $post['introtext'] = JRequest::getVar('introtext', '', 'post', 'string', JREQUEST_ALLOWRAW);
        $post['fulltext'] = JRequest::getVar('fulltext', '', 'post', 'string', JREQUEST_ALLOWRAW);
        $post['title_alias'] = "abc-abc";
        $post['mask'] = 0;
        $post['created_by_alias'] = '';
        $post['checked_out'] = 0;
        $post['checked_out_time'] = '0000-00-00 00:00:00';
        $post['publish_up'] = gmdate('Y-m-d H:i:s');
        $post['publish_down'] = '0000-00-00 00:00:00';
        $post['images'] = '';
        $post['urls'] = '';
        $post['attribs'] = 'test';
        $post['version'] = 1;
        $post['parentid'] = 0;
        $post['ordering'] = 5;
        $post['metakey'] = '';
        $post['metadesc'] = '';
        $post['access'] = 0;
        $post['hits'] = 1;
        $post['metadata'] = 'robots';
        if ($post['id'] != 0) {
            $post['modified'] = gmdate('Y-m-d H:i:s');
            $post['modified_by'] = (int) $user->get('id');
        } else {
            $post['created'] = gmdate('Y-m-d H:i:s');
            $post['created_by'] = (int) $user->get('id');
        }

        $model = $this->getModel('articleflow_edit');
        if ($model->storeAs($post)) {
            $msg = JText::_('News Content Saved');
        } else {
            $msg = JText::_('Error Saving Article Flow');
        }
        // Xoa cac bai viet chinh sua truoc khi cong bo len website
        if (!$model->del_content_temp_edit($cid)) {
            echo "<script> alert('" . $model->getError(true) . "'); window.history.go(-1); </script>\n";
            if (!$model->delete($cid)) {
                echo "<script> alert('" . $model->getError(true) . "'); window.history.go(-1); </script>\n";
            }
            if (!$model->delete_comment_byArticleID($cid)) {
                echo "<script> alert('" . $model->getError(true) . "'); window.history.go(-1); </script>\n";
            }
        }
        //ArticleflowControllerArticleflow::save();
        $this->setRedirect('index.php?option=com_articleflow', $msg);
    }

    function SendPublished() {
        JRequest::checkToken() or jexit('Invaild Token');
        $user = & JFactory::getUser();
        $model = $this->getModel('articleflow_edit');
        $post = JRequest::get('post');
        $cid = JRequest::getVar('cid', array(0), 'post', 'array');
        $post['id'] = (int) $cid[0];
        // print_r($post); die();
        $post['introtext'] = JRequest::getVar('introtext', '', 'post', 'string', JREQUEST_ALLOWRAW);
        $post['fulltext'] = JRequest::getVar('fulltext', '', 'post', 'string', JREQUEST_ALLOWRAW);

        $assUserId = $post['assigned_user'];
        $role = ArticleflowHelper::getUserRole($assUserId);

        if ($post['id'] != 0) {
            $post['modified'] = gmdate('Y-m-d H:i:s');
            $post['modified_by'] = (int) $user->get('id');
        } else {

            $post['created'] = gmdate('Y-m-d H:i:s');
            $post['created_by'] = (int) $user->get('id');
        }

        if ($role == 2) {
            $post['state'] = 2;
            $post_temp = $post;
            $post_temp['id'] = 0;
            $post_temp['content_temp_id'] = $post['id'];
            $post_temp['created'] = gmdate('Y-m-d H:i:s');
            $post_temp['created_by'] = (int) $user->get('id');
        } else if ($role == 0) {

            //die();
            $post['state'] = 0;
            $post_temp = $post;
            $post_temp['id'] = 0;
            $post_temp['content_temp_id'] = $post['id'];
            $post_temp['created'] = gmdate('Y-m-d H:i:s');
            $post_temp['created_by'] = (int) $user->get('id');
        } else if ($role == 1) {
            $post['state'] = 1;
            $post_temp = $post;
            $post_temp['id'] = 0;
            $post_temp['content_temp_id'] = $post['id'];
            $post_temp['created'] = gmdate('Y-m-d H:i:s');
            $post_temp['created_by'] = (int) $user->get('id');
        }

        //Luu vao bang Content_temp_edit
        $model->saveContentEdit($post_temp);


        if ($model->store($post)) {
            $msg = JText::_('Article Flow Sent');
        } else {
            $msg = JText::_('Error Sending Article Flow');
        }

        $this->setRedirect('index.php?option=com_articleflow', $msg);
    }

    function ViewHistory() {
        $cid = JRequest::getVar('cid', array(0), 'post', 'array');
        $post = (int) $cid[0];
        // echo 'Controller  ID'. $post;  die();
        $this->setRedirect('index.php?option=com_articleflow&view=history&cid=' . $post);
    }

    function remove() {
        JRequest::checkToken() or jexit('Invaild Token');

        $cid = JRequest::getVar('cid', array(), 'post', 'array');

        JArrayHelper::toInteger($cid);

        if (count($cid) < 1) {
            JError::raiseError(500, JText::_('Select an item to delete'));
        }

        $model = $this->getModel('articleflow_edit');
        if ($model->check_content_temp_edit($cid)) {
            $model->del_content_temp_edit($cid);
        }
        if (!$model->delete_comment_byArticleID($cid)) {
            echo "<script> alert('" . $model->getError(true) . "'); window.history.go(-1); </script>\n";
        }

        if (!$model->delete($cid)) {
            echo "<script> alert('" . $model->getError(true) . "'); window.history.go(-1); </script>\n";
        }
        $this->setRedirect('index.php?option=com_articleflow');
    }

    function publish() {
        JRequest::checkToken() or jexit('Invalid Token');

        $cid = JRequest::getVar('cid', array(), 'post', 'array');
        JArrayHelper::toInteger($cid);

        if (count($cid) < 1) {
            JError::raiseError(500, JText::_('Select an item to publish'));
        }

        $model = $this->getModel('articleflow_edit');
        if (!$model->publish($cid, 1)) {
            echo "<script> alert('" . $model->getError(true) . "'); window.history.go(-1); </script>\n";
        }

        $this->setRedirect('index.php?option=com_articleflow');
    }

    function unpublish() {
        JRequest::checkToken() or jexit('Invaild Token');
        $cid = JRequest::getVar('cid', array(), 'post', 'array');

        JArrayHelper::toInteger($cid);

        if (count($cid) < 1) {
            JError::raiseError(500, JText::_('Select an item to unpublish'));
        }

        $model = $this->getModel('articleflow_edit');

        if (!$model->publish($cid, 0)) {
            echo "<script> alert('" . $model->getError(true) . "'); window.history.go(-1); </script>\n";
        }
        $this->setRedirect('index.php?option=com_articleflow');
    }

    function comment() {
        $user = JFactory::getUser();
        $post = JRequest::get('post');
        $post['user_sent'] = $user->id;
        $post['create_date'] = gmdate('Y-m-d H:i:s');

        $model = $this->getModel('articleflow_edit');
        $model->saveComment($post);




//$user_sent= $user->id;
        // $date_create = gmdate('Y-m-d H:i:s');
//     echo 'test data   ' . $user_to . 'ma bai viet ' . $cid;
//     die();
//     
    }

}

?>
